<?php

namespace app\index\controller;

use app\commons\model\Material as MaterialModel;
class Material extends Main
{
    /**
     * 添加素材
     */
    public function create(){
        $MaterialModel = new MaterialModel();
        $sale = \app\commons\model\Sale::where(['id'=>$this->data['sale_id'],'member_id'=>$this->merch_id])->find();
        if(!$sale) jsonp_error(422,'非法操作！');
        if(!empty($this->data)){
            $this->data['user_id'] = $this->merch_id;
            $this->data['is_upload_yun'] = 0;
            $this->data['yun_type'] = 0;
            $this->data['position'] = $this->data['position'] ?: 0;
            $res = $MaterialModel->allowField(true)->save($this->data);
            if(!$res) jsonp_error(422,$MaterialModel->getError());
            jsonp([]);
        }
        jsonp_error(422,'添加失败！');
    }

    /**
     * 删除素材
     */
    public function delete(){
        $id = $this->data['id'];
        $res = MaterialModel::destroy(function ($query) use ($id){
            $query->where(['user_id'=>$this->merch_id,'id'=>$id]);
        });
        if(!$res) jsonp_error(422,'删除失败！');
        jsonp([]);
    }
}